home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / mysql / scripts / mysqld_safe < prev    next >
Text File  |  2005-04-01  |  11KB  |  362 lines

  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # scripts to start the MySQL daemon and restart it if it dies unexpectedly
  6. #
  7. # This should be executed in the MySQL base directory if you are using a
  8. # binary installation that has other paths than you are using.
  9. #
  10. # mysql.server works by first doing a cd to the base directory and from there
  11. # executing mysqld_safe
  12.  
  13. KILL_MYSQLD=1;
  14.  
  15. trap '' 1 2 3 15            # we shouldn't let anyone kill us
  16.  
  17. umask 007
  18.  
  19. defaults=
  20. case "$1" in
  21.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  22.       defaults="$1"; shift
  23.       ;;
  24. esac
  25.  
  26. parse_arguments() {
  27.   # We only need to pass arguments through to the server if we don't
  28.   # handle them here.  So, we collect unrecognized options (passed on
  29.   # the command line) into the args variable.
  30.   pick_args=
  31.   if test "$1" = PICK-ARGS-FROM-ARGV
  32.   then
  33.     pick_args=1
  34.     shift
  35.   fi
  36.  
  37.   for arg do
  38.     case "$arg" in
  39.       --skip-kill-mysqld*)
  40.         KILL_MYSQLD=0;
  41.         ;;
  42.       # these get passed explicitly to mysqld
  43.       --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--basedir=;;"` ;;
  44.       --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--datadir=;;"` ;;
  45.       --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"` ;;
  46.       --user=*) user=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; SET_USER=1 ;;
  47.  
  48.       # these two might have been set in a [mysqld_safe] section of my.cnf
  49.       # they are added to mysqld command line to override settings from my.cnf
  50.       --socket=*)  mysql_unix_port=`echo "$arg" | sed -e "s;--socket=;;"` ;;
  51.       --port=*)    mysql_tcp_port=`echo "$arg" | sed -e "s;--port=;;"` ;;
  52.  
  53.       # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
  54.       --ledir=*)   ledir=`echo "$arg" | sed -e "s;--ledir=;;"` ;;
  55.       # err-log should be removed in 5.0
  56.       --err-log=*) err_log=`echo "$arg" | sed -e "s;--err-log=;;"` ;;
  57.       --log-error=*) err_log=`echo "$arg" | sed -e "s;--log-error=;;"` ;;
  58.       # QQ The --open-files should be removed in 5.0
  59.       --open-files=*) open_files=`echo "$arg" | sed -e "s;--open-files=;;"` ;;
  60.       --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--open-files-limit=;;"` ;;
  61.       --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--core-file-size=;;"` ;;
  62.       --timezone=*) TZ=`echo "$arg" | sed -e "s;--timezone=;;"` ; export TZ; ;;
  63.       --mysqld=*)   MYSQLD=`echo "$arg" | sed -e "s;--mysqld=;;"` ;;
  64.       --mysqld-version=*)
  65.     tmp=`echo "$arg" | sed -e "s;--mysqld-version=;;"`
  66.     if test -n "$tmp"
  67.     then
  68.       MYSQLD="mysqld-$tmp"
  69.     else
  70.       MYSQLD="mysqld"
  71.     fi
  72.     ;;
  73.       --nice=*) niceness=`echo "$arg" | sed -e "s;--nice=;;"` ;;
  74.       *)
  75.         if test -n "$pick_args"
  76.         then
  77.           # This sed command makes sure that any special chars are quoted,
  78.           # so the arg gets passed exactly to the server.
  79.           args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  80.         fi
  81.         ;;
  82.     esac
  83.   done
  84. }
  85.  
  86.  
  87. MY_PWD=`pwd`
  88. # Check if we are starting this relative (for the binary release)
  89. if test -f ./share/mysql/english/errmsg.sys -a \
  90.  -x ./bin/mysqld
  91. then
  92.   MY_BASEDIR_VERSION=$MY_PWD        # Where bin, share and data are
  93.   ledir=$MY_BASEDIR_VERSION/bin        # Where mysqld is
  94.   DATADIR=$MY_BASEDIR_VERSION/data
  95.   if test -z "$defaults"
  96.   then
  97.     defaults="--defaults-extra-file=$MY_BASEDIR_VERSION/data/my.cnf"
  98.   fi
  99. # Check if this is a 'moved install directory'
  100. elif test -f ./share/mysql/english/errmsg.sys -a \
  101.  -x ./libexec/mysqld
  102. then
  103.   MY_BASEDIR_VERSION=$MY_PWD        # Where libexec, share and var are
  104.   ledir=$MY_BASEDIR_VERSION/libexec    # Where mysqld is
  105.   DATADIR=$MY_BASEDIR_VERSION/var
  106. else
  107.   MY_BASEDIR_VERSION=/usr/local
  108.   DATADIR=/usr/local/var
  109.   ledir=/usr/local/libexec
  110. fi
  111.  
  112. user=mysql
  113. niceness=0
  114.  
  115. # Use the mysqld-max binary by default if the user doesn't specify a binary
  116. if test -x $ledir/mysqld-max
  117. then
  118.   MYSQLD=mysqld-max
  119. else
  120.   MYSQLD=mysqld
  121. fi
  122.  
  123. # these rely on $DATADIR by default, so we'll set them later on
  124. pid_file=
  125. err_log=
  126.  
  127. # Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
  128. # and then merge with the command line arguments
  129. if test -x ./bin/my_print_defaults
  130. then
  131.   print_defaults="./bin/my_print_defaults"
  132. elif test -x /usr/local/bin/my_print_defaults
  133. then
  134.   print_defaults="/usr/local/bin/my_print_defaults"
  135. elif test -x /usr/local/bin/mysql_print_defaults
  136. then
  137.   print_defaults="/usr/local/bin/mysql_print_defaults"
  138. else
  139.   print_defaults="my_print_defaults"
  140. fi
  141.  
  142. args=
  143. SET_USER=2
  144. parse_arguments `$print_defaults $defaults --loose-verbose mysqld server`
  145. if test $SET_USER -eq 2
  146. then
  147.   SET_USER=0
  148. fi
  149. parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld`
  150. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  151. safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-/tmp/mysql.sock}}
  152.  
  153. # Make sure that directory for $safe_mysql_unix_port exists
  154. mysql_unix_port_dir=`dirname $safe_mysql_unix_port`
  155. if [ ! -d $mysql_unix_port_dir ]
  156. then
  157.   mkdir $mysql_unix_port_dir
  158.   chown $user $mysql_unix_port_dir
  159. fi
  160.  
  161.  
  162. if test ! -x $ledir/$MYSQLD
  163. then
  164.   echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  165.   echo "Please do a cd to the mysql installation directory and restart"
  166.   echo "this script from there as follows:"
  167.   echo "./bin/mysqld_safe".
  168.   echo "See http://dev.mysql.com/doc/mysql/en/mysqld_safe.html for more"
  169.   echo "information"
  170.   exit 1
  171. fi
  172.  
  173. if test -z "$pid_file"
  174. then
  175.   pid_file=$DATADIR/`/bin/hostname`.pid
  176. else
  177.   case "$pid_file" in
  178.     /* ) ;;
  179.     * )  pid_file="$DATADIR/$pid_file" ;;
  180.   esac
  181. fi
  182. test -z "$err_log"  && err_log=$DATADIR/`/bin/hostname`.err
  183.  
  184. if test -n "$mysql_unix_port"
  185. then
  186.   args="--socket=$mysql_unix_port $args"
  187. fi
  188. if test -n "$mysql_tcp_port"
  189. then
  190.   args="--port=$mysql_tcp_port $args"
  191. fi
  192.  
  193. if test $niceness -eq 0
  194. then
  195.   NOHUP_NICENESS="nohup"
  196. else
  197.   NOHUP_NICENESS="nohup nice -$niceness"
  198. fi
  199.  
  200. # Using nice with no args to get the niceness level is GNU-specific.
  201. # This check could be extended for other operating systems (e.g.,
  202. # BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
  203. # But, it also seems that GNU nohup is the only one which messes
  204. # with the priority, so this is okay.
  205. if nohup nice > /dev/null 2>&1
  206. then
  207.     normal_niceness=`nice`
  208.     nohup_niceness=`nohup nice`
  209.  
  210.     numeric_nice_values=1
  211.     for val in $normal_niceness $nohup_niceness
  212.     do
  213.         case "$val" in
  214.             -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
  215.              [0-9] |  [0-9][0-9] |  [0-9][0-9][0-9] )
  216.                 ;;
  217.             * )
  218.                 numeric_nice_values=0 ;;
  219.         esac
  220.     done
  221.  
  222.     if test $numeric_nice_values -eq 1
  223.     then
  224.         nice_value_diff=`expr $nohup_niceness - $normal_niceness`
  225.         if test $? -eq 0 && test $nice_value_diff -gt 0 && \
  226.             nice --$nice_value_diff echo testing > /dev/null 2>&1
  227.         then
  228.             # nohup increases the priority (bad), and we are permitted
  229.             # to lower the priority with respect to the value the user
  230.             # might have been given
  231.             niceness=`expr $niceness - $nice_value_diff`
  232.             NOHUP_NICENESS="nice -$niceness nohup"
  233.         fi
  234.     fi
  235. else
  236.     if nohup echo testing > /dev/null 2>&1
  237.     then
  238.         :
  239.     else
  240.         # nohup doesn't work on this system
  241.         NOHUP_NICENESS=""
  242.     fi
  243. fi
  244.  
  245. USER_OPTION=""
  246. if test -w / -o "$USER" = "root"
  247. then
  248.   if test "$user" != "root" -o $SET_USER = 1
  249.   then
  250.     USER_OPTION="--user=$user"
  251.   fi
  252.   # If we are root, change the err log to the right user.
  253.   touch $err_log; chown $user $err_log
  254.   if test -n "$open_files"
  255.   then
  256.     ulimit -n $open_files
  257.     args="--open-files-limit=$open_files $args"
  258.   fi
  259.   if test -n "$core_file_size"
  260.   then
  261.     ulimit -c $core_file_size
  262.   fi
  263. fi
  264.  
  265. #
  266. # If there exists an old pid file, check if the daemon is already running
  267. # Note: The switches to 'ps' may depend on your operating system
  268. if test -f $pid_file
  269. then
  270.   PID=`cat $pid_file`
  271.   if /bin/kill -0 $PID > /dev/null 2> /dev/null
  272.   then
  273.     if /bin/ps p $PID | grep mysqld > /dev/null
  274.     then    # The pid contains a mysqld process
  275.       echo "A mysqld process already exists"
  276.       echo "A mysqld process already exists at " `date` >> $err_log
  277.       exit 1
  278.     fi
  279.   fi
  280.   rm -f $pid_file
  281.   if test -f $pid_file
  282.   then
  283.     echo "Fatal error: Can't remove the pid file: $pid_file"
  284.     echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  285.     echo "Please remove it manually and start $0 again"
  286.     echo "mysqld daemon not started"
  287.     exit 1
  288.   fi
  289. fi
  290.  
  291. #
  292. # Uncomment the following lines if you want all tables to be automatically
  293. # checked and repaired during startup. You should add sensible key_buffer
  294. # and sort_buffer values to my.cnf to improve check performance or require
  295. # less disk space.
  296. # Alternatively, you can start mysqld with the "myisam-recover" option. See
  297. # the manual for details.
  298. #
  299. # echo "Checking tables in $DATADIR"
  300. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
  301. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
  302.  
  303. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  304.  
  305. # Does this work on all systems?
  306. #if type ulimit | grep "shell builtin" > /dev/null
  307. #then
  308. #  ulimit -n 256 > /dev/null 2>&1        # Fix for BSD and FreeBSD systems
  309. #fi
  310.  
  311. echo "`date +'%y%m%d %H:%M:%S  mysqld started'`" >> $err_log
  312. while true
  313. do
  314.   rm -f $safe_mysql_unix_port $pid_file    # Some extra safety
  315.   if test -z "$args"
  316.   then
  317.     $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file --skip-locking >> $err_log 2>&1
  318.   else
  319.     eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file --skip-locking $args >> $err_log 2>&1"
  320.   fi
  321.   if test ! -f $pid_file        # This is removed if normal shutdown
  322.   then
  323.     echo "STOPPING server from pid file $pid_file"
  324.     break
  325.   fi
  326.  
  327.   if true && test $KILL_MYSQLD -eq 1
  328.   then
  329.     # Test if one process was hanging.
  330.     # This is only a fix for Linux (running as base 3 mysqld processes)
  331.     # but should work for the rest of the servers.
  332.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  333.     # kill -9 is used or the process won't react on the kill.
  334.     numofproces=`ps xaww | grep -v "grep" | grep "$ledir/$MYSQLD\>" | grep -c "pid-file=$pid_file"`
  335.  
  336.     echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  337.     I=1
  338.     while test "$I" -le "$numofproces"
  339.     do 
  340.       PROC=`ps xaww | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'` 
  341.  
  342.       for T in $PROC
  343.       do
  344.         break
  345.       done
  346.       #    echo "TEST $I - $T **"
  347.       if kill -9 $T
  348.       then
  349.         echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  350.       else 
  351.         break
  352.       fi
  353.       I=`expr $I + 1`
  354.     done
  355.   fi
  356.   echo "`date +'%y%m%d %H:%M:%S'`  mysqld restarted" | tee -a $err_log
  357. done
  358.  
  359. echo "`date +'%y%m%d %H:%M:%S'`  mysqld ended" | tee -a $err_log
  360. echo "" | tee -a $err_log
  361.  
  362.